<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 系统广告 模型
 */
class Advert_model extends MY_Model {

    protected $_table      = 'advert';
    protected $primary_key = 'id';
    protected $return_type = 'array';

    public function __construct() {
        parent::__construct();
        log_message('debug', 'Advert_model Model Class Initialized');
    }

    /***************************** 广告 *****************************/

    /**
     * 获取 广告 信息
     *
     * 暂时不加 type = 1 广告位类型查询
     *
     * @param int $id
     * @return array
     */
    public function get_advert($id) {
        if (empty($id)) return array();
        //$this->where_advert();
        return $this->_build_advert($this->get($id));
    }

    /**
     * 获取 广告 列表
     *
     * @param int $limit
     * @param int $start
     * @param array $where
     * @return array
     */
    public function get_adverts($limit, $start, $where) {
        $adverts = array();
        $query = $this->where_advert($where)->order_by('vieworder ASC, id DESC')->get($this->_table, $limit, $start);
        foreach ($query->result_array() as $row) {
            $adverts[$row[$this->primary_key]] = $this->_build_advert($row);
        }
        return $adverts;
    }

    /**
     * 获取 广告 数量
     *
     * @param array $where
     * @return int
     */
    public function get_adverts_count($where) {
        return $this->where_advert($where)->count_all_results($this->_table);
    }

    /**
     * 重建 广告 行数据
     * @param array $row 【广告】一行数据
     * @return array
     */
    private function _build_advert($row) {
        $row['config'] = unserialize($row['config']);
        return $row;
    }

    /**
     * 重建 广告 查询
     *
     * @param array $where
     * @return void
     */
    public function where_advert($where = array()) {
        //type = 1 为广告
        $this->db->where('type', 1);
        //广告位
        if (!empty($where['ckey']) && $where['ckey'] != '全部') {
            $this->db->where('ckey', $where['ckey']);
        }
        //广告状态
        $status = array('open' => 1, 'close' => 0);
        if (!empty($where['status']) && isset($status[$where['status']])) {
            $this->db->where('status', $status[$where['status']]);
        }
        //描述
        if (!empty($where['descrip'])) {
            $this->db->like('descrip', $where['descrip']);
        }
        //广告类型
        if (!empty($where['advert_type']) && $where['advert_type'] != '全部') {
            $this->db->like('config', $where['advert_type']);
        }
        return $this->db;
    }

    /***************************** 广告位 *****************************/

    /**
     * 获取 广告位 信息
     *
     * 暂时不加 type = 0 广告位类型查询
     *
     * @param int $id
     * @return array
     */
    public function get_advert_cate($id) {
        if (empty($id)) return array();
        //$this->where_advert_cate();
        return $this->_build_advert_cate($this->advert_model->get($id));
    }

    /**
     * 获取 广告位 列表
     *
     * @param int $limit
     * @param int $start
     * @param array $where
     * @return array
     */
    public function get_advert_cates($limit = null, $start = null, $where = array()) {
        $cates = array();
        $query = $this->where_advert_cate($where)->order_by('vieworder ASC, id DESC')->get($this->_table, $limit, $start);
        foreach ($query->result_array() as $row) {
            $cates[$row['ckey']] = $this->_build_advert_cate($row);
        }
        return $cates;
    }

    /**
     * 获取 广告 数量
     *
     * @param array $where
     * @return int
     */
    public function get_advert_cates_count($where) {
        return $this->where_advert_cate($where)->count_all_results($this->_table);
    }

    /**
     * 获取 广告位 key => name
     *
     * @return array(ckey => name, ckey => name);
     */
    public function get_advert_cates_dropdown() {
        $cates = array();
        foreach ($this->get_advert_cates() as $cate) {
            $cates[$cate['ckey']] = $cate['name'];
        }
        return $cates;
    }

    /**
     * 重建 广告位 行数据
     *
     * @param array $row 广告位 一行数据
     * @return array
     */
    private function _build_advert_cate($row) {
        if ($row['type'] == 0) {
            $descrip = explode("~\t~", $row['descrip']);
            $row['name'] = isset($descrip[0]) ? $descrip[0] : '';
            $row['descrip'] = isset($descrip[1]) ? $descrip[1] : '';
        }
        $row['config'] = unserialize($row['config']);
        return $row;
    }

    /**
     * 重建 广告位 查询
     *
     * @param array $where
     * @return void
     */
    public function where_advert_cate($where = array()) {
        //type = 0 为广告位
        $this->db->where('type', 0);
        //广告位状态
        $status = array('open' => 1, 'close' => 0);
        if (!empty($where['status']) && isset($status[$where['status']])) {
            $this->db->where('status', $status[$where['status']]);
        }
        //广告位描述
        if (!empty($where['descrip'])) {
            $this->db->like('descrip', $where['descrip']);
        }
        return $this->db;
    }

    /***************************** 其他 *****************************/

    /**
     * 获取广告类型
     */
    public function get_advert_types() {
        $type = array();
        $type['txt'] = '文字';
        $type['img'] = '图片';
        $type['code'] = '代码';
        $type['flash'] = 'Flash';
        return $type;
    }

    /**
     * config 字段默认数据
     */
    public function get_default_advert_config() {
        $config = array();
        //文字
        $config['txt']['title'] = ''; //文字内容
        $config['txt']['link'] = ''; //文字链接
        $config['txt']['color'] = '#000000'; //文字颜色
        $config['txt']['size'] = '12'; //文字大小[像素]
        //图片
        $config['img']['url'] = ''; //图片地址
        $config['img']['link'] = ''; //图片链接
        $config['img']['title'] = ''; //图片提示
        $config['img']['width'] = ''; //图片宽高
        $config['img']['height'] = ''; //图片宽高
        //代码
        $config['code']['htmlcode'] = ''; //广告代码
        //Flash
        $config['flash']['link'] = ''; //Flash链接
        $config['flash']['width'] = ''; //Flash宽高
        $config['flash']['height'] = ''; //Flash宽高
        return $config;
    }
}

/* End of file advert_model.php */
/* Location: ./application/models/advert_model.php */
